package com.transcargo.geo;

import java.util.ArrayList;
import java.util.Iterator;

import com.transcargo.db.db;
import com.transcargo.db.dbException;

public class Country {

	private int Id = -1;
	private Region[] regions = null;
	private City[] cities = null;
	private int num_regions = 0;
	private int num_cities = 0;
	private String name = "";
	public Country(int id, Boolean initRegions, db Db) throws GeoObjectNotFoundException {
		Id = id;
        try {
            if (Db != null) {
                ArrayList<String[]> list = Db.execute("SELECT name FROM countries WHERE id=" + Integer.toString(id));
                if (list != null) {
                    Iterator<String[]> iter = list.iterator();
                    if (iter == null || !iter.hasNext()) throw new GeoObjectNotFoundException(new com.transcargo.geo.Object(this, null, null));
                    String[] ctr = iter.next();
                    name = ctr[0];
                    if (initRegions) {
                        list = Db.execute("SELECT reg_id FROM regions WHERE country_id=" + Integer.toString(id));
                        if (list != null) {
                            iter =list.iterator();
                            num_regions = list.size();
                            regions = new Region[num_regions];
                            int i = 0;
                            while (iter.hasNext()) {
                                ctr = iter.next();
                                int reg_id = Integer.parseInt(ctr[0]);
                                regions[i] = new Region(reg_id, id, Db);
                                i++;
                            }
                        }
                        list = Db.execute("SELECT city_id FROM cities WHERE country_id=" + Integer.toString(id));
                        if (list != null) {
                            iter =list.iterator();
                            num_cities = list.size();
                            cities = new City[num_cities];
                            int i = 0;
                            while (iter.hasNext()) {
                                ctr = iter.next();
                                int city_id = Integer.parseInt(ctr[0]);
                                cities[i] = new City(city_id, id, Db);
                                i++;
                            }
                        }
                    }
                } else throw new GeoObjectNotFoundException(new com.transcargo.geo.Object(this, null, null));
            }
        } catch (dbException e) {
            throw new GeoObjectNotFoundException(new com.transcargo.geo.Object(this, null, null));
        }
    }
	public Region[] getRegions() {
		return regions;
	}
	public Region getRegion(int id) {
		if (regions != null) for (int i = 0; i < regions.length; i++) if (regions[i].getId() == id) return regions[i];
		return null;
	}
	public City[] getCities() {
		return cities;
	}
	public int getNumCities() {
		return num_cities;
	}
	public int getNumRegions() {
		return num_regions;
	}
	public String getName() {
		return name;
	}
    public int getId() {
        return Id;
    }
}
